/**
 * Description: 登录
 *
 * @author      Eden
 * @datetime    2016/11/25 09:30
 * @copyright   Beijing CmsTop Technology Co.,Ltd.
 */

(function () {
    "use strict";

    // 登录页控制器
    sm.controller('admin-login-index', ['$scope', '$http', function ($scope, $http) {

        var api = {
            "loginUrl": "/login/checkLogin"
        };

        $scope.loginType = 'base';
        $scope.showBtnWords = true;
        $scope.loginLock = false;
        $scope.login = {
            "email": "",
            "password": "",
            "error": ""
        };

        $scope.setLoginType = function (loginType) {
            if (loginType == $scope.loginType) {
                return
            }

            $scope.login.email = "";
            $scope.login.password = "";
            $scope.login.error = "";

            $scope.loginType = loginType
        };

        $scope.loginAction = function () {

            // 验证用户名和密码
            var pregExp = new RegExp(/^[-a-z0-9~!$%^&*_=+}{\'?]+(\.[-a-z0-9~!$%^&*_=+}{\'?]+)*@([a-z0-9_][-a-z0-9_]*(\.[-a-z0-9_]+)*\.(aero|arpa|biz|com|coop|edu|gov|info|int|mil|museum|name|net|org|pro|travel|mobi|[a-z][a-z])|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,5})?$/i);

            // 邮箱和密码是否为空
            if (!$scope.login.email || !$scope.login.password) {
                $scope.login.error = '邮箱或密码不能为空';
                return false;
            }

            // 邮箱格式
            if (!pregExp.test($scope.login.email)) {
                $scope.login.error = '邮箱格式不匹配';
                return false;
            }

            // 密码长度
            if ($scope.login.password.length < 6 || $scope.login.password.length > 18) {
                $scope.login.error = '邮箱或密码错误';
                return false;
            }

            // 执行登录检查
            var data = {
                type: $scope.loginType,
                email: $scope.login.email,
                password: $scope.login.password
            };

            // $scope.loginLock = true;
            $scope.showBtnWords = false;
            $http.post(api.loginUrl, data).success(function (res) {
                if (res.state) {
                    window.location.href = '/home';
                } else {
                    $scope.showBtnWords = true;
                    var error = res.error ? res.error : '用户名和密码错误';
                    $scope.login.error = error;
                }
            });


        };


    }]);

})();

